Request এবং Response Model কনফিগারেশন

Web Development - ফাস্টএপিআই (FastAPI) - FastAPI এর বেসিক কনফিগারেশন
180

FastAPI-তে Request Model এবং Response Model ব্যবহার করে ইনপুট ও আউটপুট ডাটা স্ট্রাকচার সহজেই সংজ্ঞায়িত করা যায়। Pydantic ব্যবহার করে এই মডেলগুলো তৈরি করা হয়, যা ডাটা যাচাই (validation) এবং টাইপিং নিশ্চিত করে।


Request Model কনফিগারেশন

Request Model তৈরি করা

Pydantic-এর BaseModel ব্যবহার করে Request Model তৈরি করা হয়। এটি ইনপুট ডাটার কাঠামো এবং যাচাই নির্ধারণ করে।

from pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

Request Model ব্যবহার করা

FastAPI এন্ডপয়েন্টে Request Model ইনপুট হিসেবে ব্যবহার করা যায়।

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

@app.post("/items/")
def create_item(item: Item):
    return {"item": item}
ইনপুট উদাহরণ:

POST /items/
Body:

{
  "name": "Laptop",
  "description": "A high-end gaming laptop",
  "price": 1500.00,
  "tax": 120.00
}
রেসপন্স উদাহরণ:
{
  "item": {
    "name": "Laptop",
    "description": "A high-end gaming laptop",
    "price": 1500.00,
    "tax": 120.00
  }
}

Response Model কনফিগারেশন

Response Model তৈরি করা

Response Model নির্ধারণ করতে BaseModel ব্যবহার করা হয়। এটি API-এর রেসপন্স ডাটার কাঠামো নিশ্চিত করে।

from pydantic import BaseModel

class ItemResponse(BaseModel):
    name: str
    price_with_tax: float

Response Model ব্যবহার করা

Response Model response_model প্যারামিটারের মাধ্যমে FastAPI এন্ডপয়েন্টে যুক্ত করা হয়।

@app.post("/items/", response_model=ItemResponse)
def create_item(item: Item):
    price_with_tax = item.price + (item.tax or 0)
    return {"name": item.name, "price_with_tax": price_with_tax}
রেসপন্স উদাহরণ:
{
  "name": "Laptop",
  "price_with_tax": 1620.00
}

Request এবং Response Model একত্রে ব্যবহার

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

class ItemResponse(BaseModel):
    name: str
    total_price: float

@app.post("/items/", response_model=ItemResponse)
def create_item(item: Item):
    total_price = item.price + (item.tax or 0)
    return {"name": item.name, "total_price": total_price}
POST রিকোয়েস্ট:

Body:

{
  "name": "Smartphone",
  "price": 1000.00,
  "tax": 100.00
}
রেসপন্স:
{
  "name": "Smartphone",
  "total_price": 1100.00
}

Model Field কাস্টমাইজেশন

Pydantic-এর ফিচার ব্যবহার করে মডেলের ফিল্ড কাস্টমাইজ করা যায়।

Field ডিফল্ট মান এবং যাচাই

from pydantic import BaseModel, Field

class Item(BaseModel):
    name: str = Field(..., example="Laptop")  # ফিল্ডের উদাহরণ
    description: str | None = Field(None, max_length=300, example="A lightweight laptop")
    price: float = Field(..., gt=0, example=999.99)  # `gt=0` দিয়ে নিশ্চিত করা হয়েছে যে দাম শূন্যের বেশি হবে
    tax: float | None = Field(None, example=99.99)

এন্ডপয়েন্টে উদাহরণ ডাটা দেখানো

FastAPI স্বয়ংক্রিয়ভাবে Swagger UI-তে উদাহরণ ডাটা প্রদর্শন করতে পারে।

@app.post("/items/", response_model=ItemResponse)
def create_item(item: Item):
    """
    এই এন্ডপয়েন্ট একটি নতুন আইটেম তৈরি করে।
    """
    total_price = item.price + (item.tax or 0)
    return {"name": item.name, "total_price": total_price}

Swagger UI এ উদাহরণ:

Request Body Example:

{
  "name": "Tablet",
  "description": "A new generation tablet",
  "price": 500.00,
  "tax": 50.00
}

Optional এবং Nested Model

Optional ফিল্ড:

class Item(BaseModel):
    name: str
    description: str | None = None  # ঐচ্ছিক ফিল্ড

Nested Model:

class Manufacturer(BaseModel):
    name: str
    country: str

class Item(BaseModel):
    name: str
    price: float
    manufacturer: Manufacturer  # Nested Model
উদাহরণ ইনপুট:
{
  "name": "Laptop",
  "price": 1500.00,
  "manufacturer": {
    "name": "TechCorp",
    "country": "USA"
  }
}

Response Data ফিল্টারিং

Response Model ব্যবহার করে ডাটা ফিল্টার করা যায়। যেমন:

class FullItem(BaseModel):
    name: str
    description: str
    price: float
    tax: float

class FilteredItem(BaseModel):
    name: str
    price: float

@app.post("/items/", response_model=FilteredItem)
def create_item(item: FullItem):
    return item
ইনপুট:
{
  "name": "Laptop",
  "description": "A powerful gaming laptop",
  "price": 1500.00,
  "tax": 120.00
}
রেসপন্স:
{
  "name": "Laptop",
  "price": 1500.00
}

Request এবং Response Model ব্যবহার করে FastAPI অ্যাপের ইনপুট এবং আউটপুট সহজেই যাচাই এবং নিয়ন্ত্রণ করা যায়। এটি ডেভেলপারদের জন্য একটি শক্তিশালী টুল, যা কোড মডুলার এবং সহজবোধ্য রাখে।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...